﻿using System;
using System.Collections.Generic;
using System.IO;
using Entity;

namespace Business
{
    public class AnalysisBusiness
    {
        public List<NetFlowEntity> ParseNetFlowData(string filePath)
        {
            // 先将文件转换为 TXT
            string txtFilePath = FileConverter.ConvertToTxt(filePath);

            List<NetFlowEntity> netFlowList = new List<NetFlowEntity>();

            try
            {
                using (StreamReader sr = new StreamReader(txtFilePath))
                {
                    string headerLine = sr.ReadLine();
                    if (headerLine == null) throw new Exception("文件为空或格式错误");

                    string[] headers = headerLine.Split('\t');

                    int srcIpIndex = Array.IndexOf(headers, "IPV4_SRC_ADDR");
                    int dstIpIndex = Array.IndexOf(headers, "IPV4_DST_ADDR");
                    int protocolIndex = Array.IndexOf(headers, "PROTOCOL_MAP");
                    int packetsIndex = Array.IndexOf(headers, "TOTAL_PKTS_EXP");
                    int bytesIndex = Array.IndexOf(headers, "TOTAL_BYTES_EXP");
                    int durationIndex = Array.IndexOf(headers, "FLOW_DURATION_MILLISECONDS");
                    int inBytesIndex = Array.IndexOf(headers, "IN_BYTES");
                    int outBytesIndex = Array.IndexOf(headers, "OUT_BYTES");
                    int inPktsIndex = Array.IndexOf(headers, "IN_PKTS");
                    int outPktsIndex = Array.IndexOf(headers, "OUT_PKTS");


                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] values = line.Split('\t');

                        try
                        {
                            NetFlowEntity netFlow = new NetFlowEntity
                            {
                                SourceIP = values[srcIpIndex],
                                DestinationIP = values[dstIpIndex],
                                Protocol = values[protocolIndex],
                                TotalPackets = int.Parse(values[packetsIndex]),
                                TotalBytes = long.Parse(values[bytesIndex]),
                                FlowDuration = double.Parse(values[durationIndex]),
                                InBytes = long.Parse(values[inBytesIndex]),
                                OutBytes = long.Parse(values[outBytesIndex]),
                                InPkts = int.Parse(values[inPktsIndex]),
                                OutPkts = int.Parse(values[outPktsIndex])

                            };

                            netFlowList.Add(netFlow);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"数据解析失败: {ex.Message}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取文件失败: {ex.Message}");
            }

            return netFlowList;
        }
        
        
        public List<NetFlowEntity> ParseNetFlowDataDuration(string filePath)
        {
            // 先将文件转换为 TXT
            string txtFilePath = FileConverter.ConvertToTxt(filePath);

            List<NetFlowEntity> netFlowList = new List<NetFlowEntity>();

            try
            {
                using (StreamReader sr = new StreamReader(txtFilePath))
                {
                    string headerLine = sr.ReadLine();
                    if (headerLine == null) throw new Exception("文件为空或格式错误");

                    string[] headers = headerLine.Split('\t');

                    int durationIndex = Array.IndexOf(headers, "FLOW_DURATION_MILLISECONDS");

                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] values = line.Split('\t');

                        try
                        {
                            NetFlowEntity netFlow = new NetFlowEntity
                            {
                                FlowDuration = double.Parse(values[durationIndex]),

                            };

                            netFlowList.Add(netFlow);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"数据解析失败: {ex.Message}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取文件失败: {ex.Message}");
            }

            return netFlowList;
        }
    }
}